package com.tao.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.tao.util.pack.HeartBeatThread;
import com.tao.util.pack.HeartSendThread;

public class ThreadLostener {

	public static Map<String, HeartSendThread> heartSendThreadMap = new HashMap<String, HeartSendThread>();
	public static Map<String, HeartBeatThread> heartBeatThreadMap = new HashMap<String, HeartBeatThread>();
	
	
	//关闭线程（生命维持线程，心跳线程有一个关闭就关闭所有）
	public static void closeThread(String threadName) {

		System.out.println("--------------- closeThread == "+threadName+" -----------------");	
		heartSendThreadMap.get(threadName).setBeat(false);
		heartSendThreadMap.remove(threadName);
		//移除已关闭单未移出Map的线程对象
		heartSendThreadMap.remove("close"+threadName);
	}
	
	public static void closeBeatThread(String threadName){
		System.out.println("--------------- closeThread == "+threadName+" -----------------");	
		heartBeatThreadMap.get(threadName).setBeat(false);
		
		heartBeatThreadMap.remove(threadName);	
		//移除已关闭单未移出Map的线程对象
		heartBeatThreadMap.remove("close"+threadName);
	}
	
	/**
	 * 重启生命维持线程
	 * @param threadName
	 */
	public static void reopen(String threadName) {
		
		HeartSendThread heartSendThread = heartSendThreadMap.get(threadName);
		heartSendThreadMap.get(threadName).setBeat(false);
		//创建新的生命维持对象
		HeartSendThread newheartSendThread = new HeartSendThread(heartSendThread.getUser());
		//设置监听器
		newheartSendThread.setmSendMsgListener(heartSendThread.getmSendMsgListener());
		newheartSendThread.setmExitSendMsgListener(heartSendThread.getmExitSendMsgListener());
		newheartSendThread.setName(newheartSendThread.getUser().getSid());
		newheartSendThread.setBeat(true);
		//为确保线程真正关闭这里只是将旧的线程对象的kye改为 “close”+key;当线程真正关闭时再移除；
		heartSendThreadMap.remove(threadName);
		heartSendThreadMap.put("close"+threadName, heartSendThread);
		//添加新的对象
		heartSendThreadMap.put(newheartSendThread.getName(), newheartSendThread);
		
		newheartSendThread.start();
	}
	
	public static void reBeat(String threadName){
		//先取出线程对象，在设置Beat为false
		HeartBeatThread heartBeatThread = heartBeatThreadMap.get(threadName);
		//Beat为false线程下次执行时会自动关闭
		heartBeatThreadMap.get(threadName).setBeat(false);
		
		HeartBeatThread newheartBeatThread = new HeartBeatThread(heartBeatThread.getWechat());
		//设置监听器
		newheartBeatThread.setHc(heartBeatThread.getHc());
		newheartBeatThread.setmStatsMsgListener(heartBeatThread.getmStatsMsgListener());
		newheartBeatThread.setmNewMsgListener(heartBeatThread.getmNewMsgListener());
		newheartBeatThread.setmExitMsgListener(heartBeatThread.getmExitMsgListener());
		//设置线程名
		newheartBeatThread.setName(newheartBeatThread.getWechat().skey);
		//为确保线程真正关闭这里只是将旧的线程对象的kye改为 “close”+key;当线程真正关闭时再移除；
		heartBeatThreadMap.remove(threadName);
		heartBeatThreadMap.put("close"+threadName, heartBeatThread);
		
		//添加新的线程对象
		heartBeatThreadMap.put(newheartBeatThread.getName(), newheartBeatThread);
	}
	
	
	/**
	 * 获取线程列表
	 */
	public static String getThreadNameList() {
		
//		Set<String> threadNameSet = heartSendThreadMap.keySet();
//		List<String> threadNameList = new ArrayList<String>();
//		threadNameList.addAll(threadNameSet);
		
		//遍历生命维持线程的Map
		List<ThreadLostenerView> threadSendList = new ArrayList<ThreadLostenerView>();
		for (HeartSendThread value : heartSendThreadMap.values()) {
			String beat = "";
			if(value.isBeat()){
				beat = "心跳已开启!";
			}else{
				
				beat = "心跳未开启!";
			}
			threadSendList.add(new ThreadLostenerView(value.getUser().getUin(),value.getUser().getNickName(),value.getName(),value.getUser().getLoginTime(),beat,"维持生命"));
		}
		
		
		
		//遍历心跳线程的Map
		List<ThreadLostenerView> threadBeatList = new ArrayList<ThreadLostenerView>();
		for (HeartBeatThread value : heartBeatThreadMap.values()) {
			String beat = "";
			if(value.isBeat()){
				beat = "心跳已开启!";
			}else{
				beat = "心跳未开启!";
			}
			threadBeatList.add(new ThreadLostenerView(value.getWechat().wxuin,"",value.getName(),value.getWechat().time,beat,"心跳"));
		}
		
		List<List<ThreadLostenerView>> threadNameList = new ArrayList<List<ThreadLostenerView>>();
		
		threadNameList.add(threadSendList);
		threadNameList.add(threadBeatList);
		
		System.out.println(new Gson().toJson(threadNameList));
		
		return new Gson().toJson(threadNameList);
	}
	
}

